package thanhlong90.it.testsocket;
import java.io.*;
import java.net.*;

class TCPChatClient {
	String dataOut = "Hello Server";
	
	public TCPChatClient() {
		
	}
	
	public void run	() {
		try {
			Socket s = new Socket("192.168.1.116", 4322);
			
			PrintWriter out = new PrintWriter(s.getOutputStream(), true);
			BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			while (true) {
				// Send
				System.out.print("Client: ");
				BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in));
				dataOut = kbin.readLine();
				out.println(dataOut);
				// Receive
				System.out.println("Server: ");
				if (dataOut.equals("quit")){
					System.out.println("client: thực hiện thoát vòng lặp!");
					break;
				}
				while (!in.ready()) {
					
				}
				System.out.println(in.readLine()); // Read one line and output it
				System.out.print("\n");
			}
			// Close
			out.close();
			in.close();
			s.close();
		} catch (Exception e) {
			System.out.print("Error!\n");
		}
	}

	public static void main(String args[]) {
		TCPChatClient client = new TCPChatClient();
		client.run();
	}
}